/**
 * Created by szc on 2016/12/31.
 */
class GunOne extends egret.DisplayObjectContainer {
    /**图片引用*/
    private bmpArr = [];
    /**图片数量*/
    private rowCount: number;
    /**stage宽*/
    private stageW: number;
    /**stage高*/
    private stageH: number;
    /**纹理本身的高度*/
    private textureHeight: number;
    /**控制滚动速度*/
    public speed: number = 55;
    private ract: number;
    private group: eui.Group;
    private symb: any;
    private initsybm: any;
    private gunCtr: any;
    private context: any;  //设置上下文;
    private runArr = [];
    private targetArr: any;  //结果元素反正的地方
    private mainContext: any;

    /**当前的元素位置*/
    private pos: number;

    public constructor(symb: any, initsybm: any, context: any, mainContext: any) {
        super();
        this.group = new eui.Group();
        this.rowCount = symb.length;
        this.symb = symb;  //所有图标的文件名
        this.initsybm = initsybm;  //初始化图标位置
        //上下文
        this.context = context;
        this.mainContext = mainContext;

        // console.log(this.initsybm);

        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }
    public getSysimg(id: any) {
        var image = this.createSym(this.symb[id]['text']);
        image['id'] = id;
        return image;
    }
    /**初始化*/
    private onAddToStage(event: egret.Event) {
        this.stageW = this.stage.stageWidth;
        this.stageH = this.stage.stageHeight;
        // this.ract = this.parent.height/3;
        this.ract = 110;
        this.removeEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);

        /**
         * 初始化相应的元素，但是不要渲染到屏幕上
         */
        // for (var i: number = 0; i < this.rowCount; i++) {
        //     var image = this.createSym(this.symb[i]['text']);
        //     image['id'] = i;
        //     this.bmpArr.push(image);
        // }

        /**
         * 控制容器的相关属性
         */
        this.group.scrollEnabled = true;
        this.group.width = 180;
        this.group.height = 340;
        // this.group.height = 338;
        this.group.y =0;
        /**
         * 完成布局的初始化
         */
        this.init();  //将初始化的符号放置滚筒上
        this.addChild(this.group);
    }

    /**开始滚动*/
    public start(s: any): void {


        // for (var i = 0; i < 4; i++) {
        //     this.runArr[i].y = (i) * this.ract;
        // }

        this.gunCtr = 0;  //将滚筒的控制信号设置为0,使其在没有变成为1,始终保持运动,不触发滚动的完成的事件
        this.pos = null;
        var point = this;


        setTimeout(function () {
            tw1.call(point);
        }, s);

        function tw1() {
            for (var i = 0; i < 4; i++) {
                var tw = egret.Tween.get(this.runArr[i]);
                tw.to({ y: this.runArr[i].y - 50 }, 100).to({ y: this.runArr[i].y }, 100).call(function () { });
            }
        }

        setTimeout(function () {
            test.call(point);
        }, 200 + s);

        function test() {

            this.removeEventListener(egret.Event.ENTER_FRAME, this.enterFrameHandler, this);
            this.addEventListener(egret.Event.ENTER_FRAME, this.enterFrameHandler, this);
        }

    }
    public setData(pos: any) {
        /**
  * 滚动停止时候的代码
  */
        this.pos = pos;
        //console.log("当前编码", this.pos);
        this.targetArr = [];
    }
    /**逐帧运动*/
    private enterFrameHandler(event: egret.Event): void {
        for (var i: number = 0; i < 4; i++) {
            var bgBmp: egret.DisplayObjectContainer = this.runArr[i];
            bgBmp.y += this.context.rotationSpeed;
        }

        // 判断超出屏幕后，回到队首，这样来实现循环反复
        var bgBmp: egret.DisplayObjectContainer = this.runArr[3];
        if (bgBmp.y > 320) {
            this.drawPanle();
        }
    }
    /**暂停滚动*/
    public pause(): void {
        this.removeEventListener(egret.Event.ENTER_FRAME, this.enterFrameHandler, this);
    }

    /**
     * 初始化当前的界面
     */
    public init(): void {
        /**
         * 计算面板一开始的位置
         */
        var pos = this.initsybm.Pos;
        this.pos = pos;
        if (this.pos < 0) {
            pos = this.rowCount + pos;
        };
        var posArr = this.getPosArr(pos);
        for (var i = 0; i < 4; i++) {
            this.runArr[i] = this.getSysimg(posArr[i]);
            this.runArr[i].y = (i - 1) * this.ract;
            this.group.addChild(this.runArr[i]);
        }
    }

    /**
     * 给定标志位生成相应的位置图标
     */
    private getPosArr(pos: number, oo: number = 4): any {
        var num = oo || 4;
        var arr = [];
        for (var i = 0; i < num; i++) {
            var tPos = pos + i;
            if (tPos < this.rowCount) {
                tPos = pos + i;
            } else {
                tPos = tPos - this.rowCount;
            }
            arr.push(tPos);
        }
        return arr;
    }

    /**
     * 绘制当前的面板
     */
    private drawPanle(): void {
        var point = this;
        if (this.gunCtr > 0 && this.pos != null) {
            var bgBmp1 = this.targetArr.pop();

            if (this.targetArr.length > 0) {
                bgBmp1.y = this.runArr[0].y - this.ract;
                this.group.addChild(bgBmp1);
                this.runArr.unshift(bgBmp1);
                //从尾部删除一个元素
                if (this.group.contains(this.runArr[4])) {
                    this.group.removeChild(this.runArr[4]);
                } else {
//                  console.log(this.runArr[3]["id"]);
                }

                this.runArr.pop();
            } else {
                this.pause();
                for (var i = 0; i < 4; i++) {
                    var tw = egret.Tween.get(this.runArr[i]);
                    tw.to({ y: this.runArr[i].y + 25 }, 100).to({ y: this.runArr[i].y }, 100).call(function () {
                        for (var i = 0; i < 4; i++) {
                            point.runArr[i].y = (i) * point.ract;
                        }
                    });
                }
                this.context.conpleteGun();
            }
        } else {
            //从头部增加一个元素
            var head_id = this.runArr[0].id;
            var temp_id = 0;
            if (head_id != 0) {
                temp_id = head_id - 1;
            } else {
                temp_id = this.rowCount - 1;
            }
            var bgBmp: egret.DisplayObjectContainer = this.getSysimg(temp_id);
            bgBmp.y = this.runArr[0].y - this.ract;
            this.group.addChild(bgBmp);
            this.runArr.unshift(bgBmp);
            //从尾部删除一个元素
            if (this.group.contains(this.runArr[4])) {
                this.group.removeChild(this.runArr[4]);
            } else {
//              console.log(this.runArr[3]["id"]);
            }
            this.runArr.pop();
        }
    }

    public setGunCtr(num: any): void {
        var num = num || 1;
        this.gunCtr = num;

        // console.log(this.gunCtr);

        if (this.context.gameWay == "speed") {
            var posArr = this.getPosArr(this.pos, 7); //快速玩法
        } else {
            var posArr = this.getPosArr(this.pos, 15); //普通玩法
        }
        for (var i = 0; i < posArr.length; i++) {
            this.targetArr[i] = this.getSysimg(posArr[i]);
        }

        /**
         * 当用户停止页面的时候，开启
         */
        if (this.context.gameSpeedSigal == 1) {
            var posArr = this.getPosArr(this.pos, 5); //快速玩法
            this.targetArr = [];
            for (var i = 0; i < posArr.length; i++) {
                this.targetArr[i] = this.getSysimg(posArr[i]);
            }
        }

        if (this.gunCtr > 1) {
           
            var posArr = this.getPosArr(this.pos, 20); //出现C1
            this.targetArr = [];
            for (var i = 0; i < posArr.length; i++) {
                this.targetArr[i] = this.getSysimg(posArr[i]);
            }
        }

    }

    private createSym(text: any): any {
        var img = new SysImg(this.ract, text);
        img.touchEnabled = true;
        // img.parent = text;
        img.addEventListener(egret.TouchEvent.TOUCH_TAP, this.openLianxian, this);
        return img;
    }

    private openLianxian(evt: egret.TouchEvent): void {
        var text = evt.target.parent.text;
        this.mainContext.soundctr.playSound("click_mp3");
        this.mainContext.openNewchildPanle("lianxian", text);
    }
    /**
     * 开启动画
     */
    public openAm(reArr: any): void {
        for (var i = 0; i < reArr.length; i++) {
            var po = reArr[i];
            if (po > 3) continue;
            this.runArr[po].openAm();
        }

    }
    /**
     * 关闭动画
     */
    public closeAm(): void {
        for (var i = 0; i < 4; i++) {
            this.runArr[i].closeAm();
        }
    }
}